package com.yl.chainStore.payrule.coupons.service

import com.yl.chainStore.common.enums.Status
import com.yl.chainStore.entity.pay.PayCustomerCoupons
import com.yl.chainStore.entity.pay.PayCustomerOrder
import com.yl.chainStore.payrule.common.DiscountComputeOrderResultVo
import com.yl.chainStore.payrule.coupons.CouponsRuleService
import com.yl.chainStore.payrule.coupons.CouponsRuleTypeEnum
import com.yl.chainStore.payrule.coupons.entity.OrderFullReductionRule
import com.yl.chainStore.pojo.vo.pay.PayCustomerCouponsSelectVo
import com.yl.chainStore.services.ICustomerInfoService
import com.yl.common.ErrorCode
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.stereotype.Service
import java.util.*

@Service
class OrderFullReductionRuleServiceImpl: CouponsRuleService() {

	@Autowired
	private lateinit var iCustomerInfoService: ICustomerInfoService

	private val ruleType = CouponsRuleTypeEnum.ORDER_FULL_REDUCTION_RULE

	/**
	 * 只能用满减最大的一张
	 */
	override fun computeOrderUseId(
		order: PayCustomerOrder,
		isUse: Boolean,
		ids: List<Long>
	): DiscountComputeOrderResultVo {
		if(ids.isEmpty()){
			return DiscountComputeOrderResultVo(returnSuccess(),order)
		}
		val userCoupons = getUserCoupons(order.customerId, ids)
		if(userCoupons.size > 1){
			return DiscountComputeOrderResultVo(
				returnError(ErrorCode.COUPONS_USE_ERROR,"只能使用一张满减券"),
				order)
		}
		val coupon = userCoupons[0]
		if(!coupon.getOtherContentObj().canUseForOrder(order)){
			return DiscountComputeOrderResultVo(
				returnError(ErrorCode.COUPONS_USE_ERROR,"满减券不支持使用"),
				order)
		}
		useCoupons(coupon,order)
		return DiscountComputeOrderResultVo(returnSuccess(),order)
	}

	override fun computeOrderNotUseId(order: PayCustomerOrder, isUse: Boolean): DiscountComputeOrderResultVo {
		val userCoupons = getUserCoupons(order.customerId, null)
		if(userCoupons.isEmpty()){
			return DiscountComputeOrderResultVo(returnSuccess(),order)
		}
		userCoupons.sortedByDescending {
			val vo = it.getOtherContentObj() as OrderFullReductionRule
			vo.disCountMoney
		}
		for(c in userCoupons){
			if(c.getOtherContentObj().canUseForOrder(order)){
				//使用优惠最大的优惠券
				useCoupons(c,order)
				break
			}
		}
		return DiscountComputeOrderResultVo(returnSuccess(),order)
	}

	private fun useCoupons(coupon:PayCustomerCoupons, order:PayCustomerOrder){
		val rule = coupon.getOtherContentObj() as OrderFullReductionRule
		var discountMoney = rule.disCountMoney
		val originalPrice = order.originalPrice
		if(order.originalPrice < rule.disCountMoney){
			order.originalPrice = 0
			discountMoney = originalPrice
		}else{
			order.originalPrice = order.originalPrice - rule.disCountMoney
		}

		if(order.details != null){
			for(d in order.details!!){
				d.realMoney = d.realMoney.times(order.originalPrice).div(originalPrice)
				d.realPrice = d.realMoney.div(d.number)
			}
		}
		coupon.realDiscountMoney = discountMoney
		order.addUseCoupons(coupon)
	}

	private fun getUserCoupons(customerId: Long, couponIds: List<Long>?): List<PayCustomerCoupons> {
		val vo = PayCustomerCouponsSelectVo(
			customerId = customerId,
			ruleType = ruleType,
			nowTime = Date(),
			companyId = getContext().companyId,
			subCompanyId = getContext().subCompanyId,
			flag = Status.NEW_ADD
		)
		if (couponIds != null && couponIds.isNotEmpty()) {
			vo.couponIds = couponIds
		}
		return iCustomerInfoService.getCustomerCouponsList(vo)
	}
}


